<template>
  <div>
    <!--:model="driver" 数据双向绑定-->
    <!--ref="driverForm" id="driverForm",给form取一个名字-->
    <!--:rules="formRules" 校验规则-->
    <el-form
      :model="driver"
      ref="driverForm"
      :rules="formRules"
      label-position="left"
      label-width="100px"
      class="demo-ruleForm login-container"
    >
      <h3 class="title">司机注册</h3>
      <el-form-item prop="name" label="姓名">
        <el-input
          type="text"
          v-model="driver.name"
          auto-complete="off"
          placeholder="请输入姓名！"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password" label="登录密码">
        <el-input
          type="text"
          v-model="driver.password"
          auto-complete="off"
          placeholder="请输入密码！"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password" label="确认密码">
        <el-input
          type="text"
          v-model="driver.passwords"
          auto-complete="off"
          placeholder="请输入密码！"
        ></el-input>
      </el-form-item>
      <el-form-item prop="tel" label="联系方式">
        <el-input
          type="text"
          v-model="driver.phone"
          auto-complete="off"
          placeholder="请输入联系电话！"
        ></el-input>
      </el-form-item>
      <el-form-item prop="tel" label="邮箱">
        <el-input
          type="text"
          v-model="driver.email"
          auto-complete="off"
          placeholder="请输入邮箱！"
        ></el-input>
      </el-form-item>
      <el-form-item prop="address" label="居住地址">
        <el-input
          type="text"
          v-model="driver.address"
          auto-complete="off"
          placeholder="请输入地址！"
        ></el-input>
      </el-form-item>
      <el-form-item prop="age" label="年龄">
        <el-input
          type="text"
          v-model="driver.age"
          auto-complete="off"
          placeholder="请输入年龄！"
        ></el-input>
      </el-form-item>
      <el-form-item prop="identity" label="身份证号">
        <el-input
          type="text"
          v-model="driver.identity"
          auto-complete="off"
          placeholder="请输入身份证号！"
        ></el-input>
      </el-form-item>
      <el-form-item prop="license" label="驾照标准">
        <el-select v-model="driver.license" clearable placeholder="请选择">
          <el-option
            v-for="item in catelist"
            :key="item"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="identityp" label="身份证正面">
        <el-upload
          class="upload-demo"
          action="http://localhost:8080/fastDfs/"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :on-success="handleSuccess"
          :file-list="fileList"
          list-type="picture"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">
            只能上传jpg/png文件，且不超过500kb
          </div>
        </el-upload>
      </el-form-item>
      <el-form-item prop="identityr" label="身份证反面">
        <el-upload
          class="upload-demo"
          action="http://localhost:8080/fastDfs/"
          :on-preview="handlePreview"
          :on-remove="handleRemover"
          :on-success="handleSuccessr"
          :file-list="fileList"
          list-type="picture"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">
            只能上传jpg/png文件，且不超过500kb
          </div>
        </el-upload>
      </el-form-item>

      <el-form-item style="width: 100%">
        <el-button
          type="primary"
          style="width: 100%"
          @click.native.prevent="settledIn"
          >入驻</el-button
        >
      </el-form-item>
    </el-form>
    <el-dialog title="选择地址" :visible.sync="dialogVisable" width="30%">
      <baidu-map :center="{ lng: 116.403765, lat: 39.91485 }" :zoom="11">
        <bm-view class="map"></bm-view>
        <bm-control :offset="{ width: '10px', height: '10px' }">
          <!--:sugStyle="{zIndex: 2100} 让搜索提示上浮-->
          <bm-auto-complete v-model="keyword" :sugStyle="{ zIndex: 2100 }">
            <div style="margin-bottom: 10px">
              <input
                id="searchInput"
                type="text"
                placeholder="请输入关键字"
                class="searchinput"
              />
              <el-button type="success" @click="selectAdrressConfirm"
                >确定</el-button
              >
            </div>
          </bm-auto-complete>
        </bm-control>
        <bm-local-search
          :keyword="keyword"
          :auto-viewport="true"
        ></bm-local-search>
      </baidu-map>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisable = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisable = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.driver.admin.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      catelist: ["A1", "A2", "A3", "B1", "B2", "C1", "C2", "C3"], //用来装驾照状态的
      keyword: "",
      dialogVisable: false,
      fileList: [],
      //driver:driver 为了做数据表单校验不要嵌套对象
      driver: {
        name: "",
        phone: "",
        address: "",
        age: "",
        identity: "",
        license: "",
        identityp: "",
        identityr: "",
        state: "0",
        password: "",
        passwords: "",
        email: "",
      },
      //前端校验，没做完
      formRules: {
        name: [{ required: true, message: "请输入姓名!", trigger: "blur" }],
      },
    };
  },
  methods: {
    selectAdrressConfirm() {
      //获取值搜索框值,设置给地址
      var searchInputV = document.getElementById("searchInput").value;
      this.driver.address = searchInputV;
      //关闭对话框
      this.dialogVisable = false;
    },
    selectAdrress() {
      this.dialogVisable = true;
    },

    //文件上传成功回调
    handleSuccess(response, file, fileList) {
      this.driver.identityp = response.resultObj;
      console.log(response.resultObj);
    },
    //文件删除
    handleRemove(file, fileList) {
      var pathFile = file.response.resultObj;
      this.$http
        .delete("http://localhost:8080/fastDfs/?path=" + pathFile)
        .then((res) => {
          if (res.data.state) {
            this.$message({
              message: "删除成功!",
              type: "success",
            });
          } else {
            this.$message({
              message: "删除失败!",
              type: "error",
            });
          }
        });
    },
    //文件上传成功回调  带有r的是反面的图片操作，
    handleSuccessr(response, file, fileList) {
      this.driver.identityr = response.resultObj;
      console.log(response.resultObj);
    },
    //文件删除 带有r的是反面的图片操作，
    handleRemover(file, fileList) {
      var pathFile = file.response.resultObj;
      this.$http
        .delete("http://localhost:8080/fastDfs/?path=" + pathFile)
        .then((res) => {
          if (res.data.state) {
            this.$message({
              message: "删除成功!",
              type: "success",
            });
          } else {
            this.$message({
              message: "删除失败!",
              type: "error",
            });
          }
        });
    },
    //图片预览
    handlePreview(file) {
      console.log(file);
    },
    //1、提交入驻
    settledIn() {
      this.$refs.driverForm.validate((valid) => {
        //校验表单成功后才做一下操作
        if (valid) {
          this.$confirm("确认入驻吗？", "提示", {}).then(() => {
            //拷贝后面对象的值到新对象,防止后面代码改动引起模型变化
            let para = Object.assign({}, this.driver); //driver 本身这个参数里面就有店铺和管理员信息
            // 为了后台好接收，封装一个对象放到里面
            console.log(para);
            //判断是否有id有就是修改,否则就是添加
            this.$http
              .post("http://localhost:8080/driver/register", para)
              .then((res) => {
                if (res.data.state) {
                  this.$message({
                    message: "操作成功!",
                    type: "success",
                  });
                  //重置表单
                  this.$refs["driverForm"].resetFields();
                  //跳转登录页面
                  this.$router.push({ path: "/login" });
                } else {
                  this.$message({
                    message: res.data.message,
                    type: "error",
                  });
                }
              });
          });
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.login-container {
  -webkit-border-radius: 5px;
  border-radius: 5px;
  -moz-border-radius: 5px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 500px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
  .title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
  }
  .remember {
    margin: 0px 0px 35px 0px;
  }
}

.map {
  width: 100%;
  height: 300px;
}
.searchinput {
  width: 300px;
  box-sizing: border-box;
  padding: 9px;
  border: 1px solid #dddee1;
  line-height: 20px;
  font-size: 16px;
  height: 38px;
  color: #333;
  position: relative;
  border-radius: 4px;
}
</style>
